package net.ptcs.litang.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ptcs.litang.dao.TicketDao;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

@WebServlet("/selectTicket")
public class SelectTicketServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置头文件
		response.setContentType("application/json; charset=utf8");
		PrintWriter out = response.getWriter();
		JSONArray dataContent = null;
		// 设置响应map
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		TicketDao ticketDAO = new TicketDao();
		try {
			dataContent = ticketDAO.selectTicketAll();
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}

		if (dataContent != null) {
			hashMap.put("code", 0);
			hashMap.put("msg", "查询成功");
			hashMap.put("count", dataContent.size());
			hashMap.put("data", dataContent);
		} else {
			hashMap.put("code", 1);
			hashMap.put("msg", "查询失败");
		}

		JSONObject json = JSONObject.fromObject(hashMap);
		out.write(json.toString());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, JSONException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}
}
